package edu.sias.controller;

import edu.sias.pojo.Post;
import edu.sias.pojo.S_User;
import edu.sias.service.SearchService;
import edu.sias.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/user/publishPost")
public class PublishPostServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        UserService userService = new UserService();
        //根据用户名获取用户信息，便于简介获取用户id，以便保存信息到文章对应的用户id字段
        S_User user = userService.findUserInfoByName((String) request.getSession().getAttribute("name"));
        //获取文章相关信息
        String title = request.getParameter("title");
        String text = request.getParameter("text");
        int category_id =Integer.parseInt(request.getParameter("category_id"));
        //将文章信息封装到post对象中
        Post post = new Post();
        post.setTitle(title);
        post.setText(text);
        post.setCategory_id(category_id);
        post.setUser_id(user.getId());
        //调用业务层
        SearchService searchService = new SearchService();
        searchService.addPost(post);
        //返回个人中心
        response.sendRedirect("/userInfo.jsp");
    }
}
